package zd.cornermemory.fragment.record;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import zd.cornermemory.App;
import zd.cornermemory.R;
import zd.cornermemory.adapter.PinAdapter;
import zd.cornermemory.bean.CjMap;
import zd.cornermemory.db.Cj;
import zd.cornermemory.ui.AoDetailActivity;
import zd.cornermemory.ui.CjDetailActivity;
import zd.cornermemory.view.SectionPinListView;

/**
 * Created by zhud on 2019/4/29.
 */

public class RecordFragment extends Fragment implements View.OnClickListener{
    private List<Cj> sings;

    private SectionPinListView sectionPinListView;
    private List<CjMap>       mData;
    private PinAdapter         mAdapter;
    private int currentPos;

    private List<Cj> cjList;//当前分组所有成绩
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_cj_jl, container, false);
        sectionPinListView = view.findViewById(R.id.list);
        currentPos = getActivity().getIntent().getExtras().getInt("currentPos");
        mData = new ArrayList<>();
        cjList = new ArrayList<>();
        initData();
        mAdapter = new PinAdapter(getActivity(), mData);
        sectionPinListView.setAdapter(mAdapter);
        mAdapter.setOnClickItem(new PinAdapter.OnClickItem() {
            @Override
            public void onClick(int position) {
                CjMap cjMap = mData.get(position);
                int size = cjMap.getCjList().size();
                if (size == 1) {
                    Intent intent = new Intent(getActivity(), CjDetailActivity.class);
                    Cj cj = cjMap.getCjList().get(0);
                    int p = 0;
                    for(int i = 0; i< cjList.size(); i++) {
                        if (cj.getId() == cjList.get(i).getId()) {
                            p = i+1;
                            break;
                        }
                    }
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("bean",cj);
                    bundle.putInt("position",p);
                    bundle.putInt("currentPos",currentPos);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    return;
                }

                switch (size) {
                    case 3:
                        gotoAoDetail(getString(R.string.avg_3),cjMap.getCjList());
                        break;
                    case 5:
                        gotoAoDetail(getString(R.string.avg_5),cjMap.getCjList());
                        break;
                    case 12:
                        gotoAoDetail(getString(R.string.avg_12),cjMap.getCjList());
                        break;
                }

            }
        });
        return view;
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            singMap.clear();
            mo3Map.clear();
            ao5Map.clear();
            ao12Map.clear();
            List<Cj> temp  = App.getInstance().getAllCjMap().get(currentPos);
            if (temp == null) {
                temp = new ArrayList<>();
            }
            cjList.clear();
            cjList.addAll(temp);
            split(cjList, 3);
            split(cjList, 5);
            split(cjList, 12);
            List<Cj> databaseSings = new ArrayList<>();
            for (Cj cj : cjList) {
                if (cj.getStatus() != 2) {
                    databaseSings.add(cj);
                }
            }
            sings = new ArrayList<>();
            for(int i =0;i<databaseSings.size();i++) {
                Cj cj = databaseSings.get(i);
                if (i == 0) {
                    sings.add(cj);
                }else{
                    long time = cj.getSingTime();
                    long singTime = sings.get(sings.size()-1).getSingTime();
                    if (time < singTime) {
                        sings.add(cj);
                    }
                }
            }

            Collections.sort(sings);
            Collections.sort(mo3Map);
            Collections.sort(ao5Map);
            Collections.sort(ao12Map);
            for (Cj cj : sings) {
                CjMap cjMap = new CjMap();
                cjMap.setAvg(cj.getSingTime().intValue());
                ArrayList<Cj> cjs = new ArrayList<>();
                cjs.add(cj);
                cjMap.setCjList(cjs);
                singMap.add(cjMap);
            }

            mData.clear();

            if (singMap!=null&&singMap.size()>0) {
                mData.add(new CjMap(getString(R.string.fast_1)));
                mData.addAll(singMap);
            }

            if (mo3Map!=null&&mo3Map.size()>0) {
                mData.add(new CjMap(getString(R.string.fast_3)));
                mData.addAll(mo3Map);
            }

            if (ao5Map!=null&&ao5Map.size()>0) {
                mData.add(new CjMap(getString(R.string.fast_5)));
                mData.addAll(ao5Map);
            }

            if (ao12Map!=null&&ao12Map.size()>0) {
                mData.add(new CjMap(getString(R.string.fast_12)));
                mData.addAll(ao12Map);
            }
            if (mAdapter != null) {
                mAdapter.notifyDataSetChanged();
            }

        }
    };

    public void initData() {
        mHandler.sendEmptyMessage(2);
    }

    private void gotoAoDetail(String titleStr,List<Cj> cjs) {
        Intent intent = new Intent(getActivity(), AoDetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("titleStr",titleStr);
        ArrayList<Cj> arrayList = new ArrayList<>();
        if (cjs != null) {
            for (Cj cj : cjs) {
                arrayList.add(cj);
            }
        }

        bundle.putSerializable("cj",arrayList);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.start_button:
                break;
        }
    }

    ArrayList<CjMap> singMap = new ArrayList<>();
    ArrayList<CjMap>     mo3Map  = new ArrayList<>();//
    ArrayList<CjMap> ao5Map  = new ArrayList<>();//
    ArrayList<CjMap> ao12Map = new ArrayList<>();//
    ArrayList<CjMap> countMap = new ArrayList<>();//每日复原次数

    private  void split(List<Cj> list, int len) {
        //传入集合长度
        int size = list.size();
        for (int i = 0; i < size; i++) {
            if (i > (size - len)) {
                break;
            }
            List<Cj> subList = list.subList(i, (i + len) > size ? size : i + len);
            int avg = getAvgInt(subList);
            if (avg != 0) {
                if (len == 3) {
                    if (mo3Map.size() == 0) {
                        mo3Map.add(new CjMap(avg, subList));
                    }else{
                        int a = mo3Map.get(mo3Map.size()-1).getAvg();
                        if (avg < a) {
                            mo3Map.add(new CjMap(avg, subList));
                        }
                    }

                } else if (len == 5) {
                    if (ao5Map.size() == 0) {
                        ao5Map.add(new CjMap(avg, subList));
                    }else{
                        int a = ao5Map.get(ao5Map.size()-1).getAvg();
                        if (avg < a) {
                            ao5Map.add(new CjMap(avg, subList));
                        }
                    }

                } else if (len == 12) {
                    if (ao12Map.size() == 0) {
                        ao12Map.add(new CjMap(avg, subList));
                    }else{
                        int a = ao12Map.get(ao12Map.size()-1).getAvg();
                        if (avg < a) {
                            ao12Map.add(new CjMap(avg, subList));
                        }
                    }
                }
            }
        }
    }


    private int getAvgInt(List<Cj> cjList) {
        List<Cj> tempList = new ArrayList<>();
        long timeCount = 0;//
        int dnfCount = 0;
        Cj max = null;
        for(int i = 0;i<cjList.size();i++) {
            Cj cj = cjList.get(i);
            if (cj.getStatus() == 2) {
                dnfCount++;
                max = cj;
            }
            timeCount+=cj.getSingTime();
            tempList.add(cj);
        }

        if (cjList.size() == 3) {
            if (dnfCount > 0) {
                return 0;
            }else {
                double avg = Math.round(timeCount/cjList.size());
                return (int)avg;
            }
        }else {
            if (dnfCount == 0) {
                Cj min = Collections.min(tempList);
                max = Collections.max(tempList);
                tempList.remove(min);
                tempList.remove(max);
                long time = 0;
                for (Cj c : tempList) {
                    time += c.getSingTime();
                }
                double avg = Math.round(time/tempList.size());
                return (int)avg;
            } else if (dnfCount == 1) {
                tempList.remove(max);
                Cj min = Collections.min(tempList);
                tempList.remove(min);
                long time = 0;
                for (Cj c : tempList) {
                    time += c.getSingTime();
                }
                double avg = Math.round(time/tempList.size());
                return (int)avg;
            }else{
                return 0;
            }
        }

    }

}
